<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="form" labelWidth="80px">
				<uni-forms-item label="任务名称" name="name" required>
					<uni-easyinput v-model="form.name" placeholder="请输入任务名称" :disabled="action == 'view'"/>
				</uni-forms-item>	
				<uni-forms-item label="开始时间" name="start_time" required>
					<uni-datetime-picker type="datetime" v-model="form.start_time" :disabled="action == 'view'"/>
				</uni-forms-item>	
				<uni-forms-item label="结束时间" name="end_time" required>
					<uni-datetime-picker type="datetime" v-model="form.end_time" :disabled="action == 'view'"/>
				</uni-forms-item>		
				<uni-forms-item label="负责人" name="user_id" required>				
					<uni-data-select v-model="form.user_id" :localdata="userOptions" :disabled="action == 'view'"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="盘点说明" name="remark">
					<uni-easyinput v-model="form.remark" type="textarea" autoHeight placeholder="请输入" :disabled="action == 'view'"/>
				</uni-forms-item>	
			</uni-forms>
			
			<view class="button-sp-area">
				<button type="primary" @click="doSave" size="mini" v-if="action == 'edit' || action == 'add'">保存</button>
				<button type="info" @click="doDetail" size="mini" v-if="action != 'add'">查看明细</button>
				<button type="primary" @click="doCheck" size="mini" v-if="action == 'check'">确认盘点结果</button>
			</view>
		</view>
	</view>
</template>

<script>
	import { selectDictLabel, parseTime, getDicts	} from '@/api/sys.js'
	import { apiGet, apiPatch, apiPut, apiDs	} from '@/api/openapi'

	export default {
		data() {
			return {
				action: 'add',
				form: {
					name:null,
					start_time: null,
					end_time: null,
					user_id: null,
					remark: null,
					status: '1'
				},
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '任务名称不能为空'
						}]
					},
					user_id: {
						rules: [{
							required: true,
							errorMessage: '负责人不能为空'
						}]
					},
					start_time: {
						rules: [{
							required: true,
							errorMessage: '开始时间不能为空'
						}]
					},
					end_time: {
						rules: [{
							required: true,
							errorMessage: '结束时间不能为空'
						}]
					}
				},
				statusOptions:[],
				userOptions:[]
			}
		},
		onLoad(option) {			
			this.init();	
			
			if(option.action){
				this.action = option.action;
			}
			let params = {"@table":"asset_stock","id":null}
			if(option.id){	
				params.id = option.id;
			}else{
				params.status ='4';
				params['@exp'] = {status:'neq'};
			}
			apiGet(params).then(response => {
				if(response.data == null){
					this.action = 'add';
				}else{
					this.form = response.data;
					if(this.form.status == '3'){
						this.action = 'check';
					}else if(this.form.status == '4'){
						this.action = 'view';
					}else{
						this.action = 'edit';
					}
				}				
			})	
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			init() {
				apiDs('app_user_select').then(response => {
					this.userOptions = response.data;
				})
				getDicts('asset_stock_status').then(response => {
					response.data.forEach(e=>{
						e.value = e.dictValue;
						e.text = e.dictLabel;
					})
					this.statusOptions = response.data;	
				});
			},
			reset() {
				this.form = {
					name:null,
					start_time: null,
					end_time: null,
					user_id: null,
					remark: null,
					status: '1'
				};
			},
			doSave(ref) {
				this.$refs.form.validate().then(res => {
					let data = {"@tags": ['asset_stock']};
					data['asset_stock'] = {...this.form};
					apiPatch(data).then(response => {
					  this.$modal.msgSuccess("操作成功");
					});			
				});
			},
			doCheck(ref) {
				uni.showModal({
					title: '操作提示',
					content: '确认后，该盘点任务将标记为完成状态，并将各资产的盘点状态同步到资产信息',
					success: function (res) {
						if (res.cancel) {
							console.log('用户点击取消');
							return;
						}
						
						let data = {"@tags": ['asset_stock'],'asset_stock': {...this.form} };
						data['asset_stock'].status = '4';
						apiPut(data).then(response => {
						  this.$modal.msgSuccess("确认成功");
						});		
					}
				});
			},
			doDetail() {
				uni.navigateTo({
					url: '/pages/asset/stock/detail?id='+this.form.id,
				});
			},
			dictLabel(dictData, dictVal) {
				return selectDictLabel(dictData, dictVal);
			}
		}
	}
</script>


<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
	
	.cell-pd {
		padding: 22rpx 10rpx;
	}

	.list-pd {
		margin-top: 10rpx;
	}
	
	/* 上传 */
	.uni-uploader {
		flex: 1;
		flex-direction: column;
	}
	.uni-uploader-head {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
	.uni-uploader-info {
		color: #B2B2B2;
	}
	.uni-uploader-body {
		margin-top: 16rpx;
	}
	.uni-uploader__files {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
	}
	.uni-uploader__file {
		margin: 10rpx;
		width: 210rpx;
		height: 210rpx;
	}
	.uni-uploader__img {
		display: block;
		width: 210rpx;
		height: 210rpx;
	}
	.uni-uploader__input-box {
		position: relative;
		margin:10rpx;
		width: 208rpx;
		height: 208rpx;
		border: 2rpx solid #D9D9D9;
	}
	.uni-uploader__input-box:before,
	.uni-uploader__input-box:after {
		content: " ";
		position: absolute;
		top: 50%;
		left: 50%;
		-webkit-transform: translate(-50%, -50%);
		transform: translate(-50%, -50%);
		background-color: #D9D9D9;
	}
	.uni-uploader__input-box:before {
		width: 4rpx;
		height: 79rpx;
	}
	.uni-uploader__input-box:after {
		width: 79rpx;
		height: 4rpx;
	}
	.uni-uploader__input-box:active {
		border-color: #999999;
	}
	.uni-uploader__input-box:active:before,
	.uni-uploader__input-box:active:after {
		background-color: #999999;
	}
	.uni-uploader__input {
		position: absolute;
		z-index: 1;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}
</style>